package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.shop.bo.WechatConfigBO;
import com.junzhiit.nengji.shop.dao.WechatConfigDAO;
import com.junzhiit.nengji.shop.model.WechatConfig;
import com.junzhiit.nengji.shop.model.enums.Is;

@Repository
public class WechatConfigDAOImpl extends AbstractBaseDAO<WechatConfig> implements WechatConfigDAO {

	@Override
	public List<WechatConfigBO> findAll(int pageNumber, int maxPageRow, WechatConfigBO wechatConfigBO) {
		StringBuffer sql = new StringBuffer();
		sql.append("select w.*,s.name as shopName,c.name as chainName from `wechat_config` w ");
		sql.append(" left join `shop` s on w.cs_id=s.id");
		sql.append(" left join chain c on c.id = w.cs_id");
		sql.append(" where w.is_delete =?");

		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());

		if (StringUtils.isNotBlank(wechatConfigBO.getAppId())) {
			sql.append(" and w.app_id like ?");
			params.setString("%" + wechatConfigBO.getAppId() + "%");
		}
		if (StringUtils.isNotBlank(wechatConfigBO.getPartner())) {
			sql.append(" and w.partner like ?");
			params.setString("%" + wechatConfigBO.getPartner() + "%");
		}
		if (StringUtils.isNoneBlank(wechatConfigBO.getShopName())) {
			sql.append(" and s.name like ?");
			params.setString("%" + wechatConfigBO.getShopName() + "%");
		}

		return queryForPagingList(sql.toString(), WechatConfigBO.class, params, pageNumber, maxPageRow);
	}

	@Override
	public Integer getTotalCount(WechatConfigBO wechatConfigBO) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(w.id) from `wechat_config` w ");
		sql.append(" left join `shop` s on w.cs_id=s.id");
		sql.append(" where w.is_delete =?");

		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());

		if (StringUtils.isNotBlank(wechatConfigBO.getAppId())) {
			sql.append(" and app_id like ?");
			params.setString("%" + wechatConfigBO.getAppId() + "%");
		}
		if (StringUtils.isNotBlank(wechatConfigBO.getPartner())) {
			sql.append(" and partner like ?");
			params.setString("%" + wechatConfigBO.getPartner() + "%");
		}
		if (StringUtils.isNoneBlank(wechatConfigBO.getShopName())) {
			sql.append(" and s.name like ?");
			params.setString("%" + wechatConfigBO.getShopName() + "%");
		}

		return queryForInt(sql.toString(), params);
	}

	@Override
	public Integer getAppIdExist(String appId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(id) from `wechat_config` where is_delete=?");

		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());

		if (StringUtils.isNotBlank(appId)) {
			sql.append(" and app_id=?");
			params.setString(appId);
		}

		return queryForInt(sql.toString(), params);
	}

	@Override
	public Integer getSecretExist(String secret) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(id) from `wechat_config` where is_delete=?");

		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());

		if (StringUtils.isNotBlank(secret)) {
			sql.append(" and secret=?");
			params.setString(secret);
		}

		return queryForInt(sql.toString(), params);
	}

	@Override
	public Integer getPartnerExist(String partner) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(id) from `wechat_config` where is_delete=?");

		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());

		if (StringUtils.isNotBlank(partner)) {
			sql.append(" and partner=?");
			params.setString(partner);
		}

		return queryForInt(sql.toString(), params);
	}

	@Override
	public Integer getPaySecretExist(String paySecret) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(id) from `wechat_config` where is_delete=?");

		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());

		if (StringUtils.isNotBlank(paySecret)) {
			sql.append(" and pay_secret=?");
			params.setString(paySecret);
		}

		return queryForInt(sql.toString(), params);
	}

	@Override
	public WechatConfigBO getWechatConfigById(String id) {
		StringBuffer sql = new StringBuffer();
		sql.append("select w.*,s.name 'shop_name' from `wechat_config` w");
		sql.append(" left join `shop` s on w.cs_id=s.id");
		sql.append(" where w.is_delete=?");
		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		if (StringUtils.isNoneBlank(id)) {
			sql.append(" and w.id =?");
			params.setString(id);
		}
		return super.query(sql.toString(), WechatConfigBO.class, params);
	}

	@Override
	public int checkShopId(String shopId) {
		String sql = "select count(id) from `wechat_config` where is_delete =? and cs_id =?";
		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		params.setString(shopId);
		return super.queryForInt(sql, params);
	}

	@Override
	public WechatConfigBO getWechatConfigByShopId(String shopId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select wc.*,s.id as shopId from wechat_config wc");
		sql.append(" inner join shop s on s.id = wc.cs_id");
		sql.append(" where wc.cs_id =? and wc.is_delete =?");
		StatementParameter params = new StatementParameter();
		params.setString(shopId);
		params.setInt(IsDelete.NO.toIntValue());
		return super.query(sql.toString(),WechatConfigBO.class,params);
	}

	@Override
	public WechatConfigBO getWechatConfigByAppId(String appId) {
		String sql = "select * from wechat_config where app_id = ? and is_delete =? ";
		StatementParameter params = new StatementParameter();
		params.setString(appId);
		params.setInt(Is.NO.toIntValue());
		return super.query(sql.toString(),WechatConfigBO.class,params);
	}

	@Override
	public WechatConfig getWechatConfigByWcId(String wcId) {
		String sql = "select * from wechat_config where wc_id = ? and is_delete =? ";
		StatementParameter params = new StatementParameter();
		params.setString(wcId);
		params.setInt(Is.NO.toIntValue());
		return super.query(sql.toString(),WechatConfig.class,params);
	}
	
	@Override
	public WechatConfig getWechatConfigByCsId(String csId) {
		String sql = "select * from wechat_config where cs_id = ? and is_delete =? ";
		StatementParameter params = new StatementParameter();
		params.setString(csId);
		params.setInt(Is.NO.toIntValue());
		return super.query(sql.toString(),WechatConfig.class,params);
	}
}
